package com.demo.chattest.chat;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.demo.chattest.R;
import com.demo.chattest.bean.MessageInfo;

import java.util.List;

/**
 * @Package com.demo.chattest.chat
 * @Describe class描述
 * @Author wugx
 * @Date 2020-09-25
 */
public class SendAudioItemProvider extends BaseItemProvider<MessageInfo, BaseViewHolder> {
    @Override
    public int viewType() {
        return 13;
    }

    @Override
    public int layout() {
        return R.layout.item_send_audio;
    }

    @Override
    public void convert(@NonNull BaseViewHolder helper, MessageInfo data, int position) {
        helper.addOnClickListener(R.id.iv_headImg, R.id.fl_audio_contain);
        helper.setText(R.id.tv_name, data.getFromUName());
        helper.setText(R.id.tv_date, data.getTime() + "");
        helper.setVisible(R.id.ll_d_n, data.getTargetType() != 3);
        Glide.with(mContext).load(data.getFromHeadImg()).into(helper.getImageView(R.id.iv_headImg));

        helper.setText(R.id.tv_voice_length, getAudioLength(data.getAudioDuration()));
        AnimationDrawable animationDrawable = (AnimationDrawable) helper.getImageView(R.id.iv_voice_anim).getDrawable();
        //媒体信息状态 0：正常 1：播放  2：暂停
        if (data.getMediaState() == 1) {
            animationDrawable.start();
        } else {
            if (animationDrawable.isRunning()) {
                animationDrawable.stop();
            }
        }
    }

    @Override
    public void convertPayloads(@NonNull BaseViewHolder helper, MessageInfo data, int position, @NonNull List<Object> payloads) {
        super.convertPayloads(helper, data, position, payloads);
        if (payloads.get(0) instanceof Bundle) {
            Bundle bundle = (Bundle) payloads.get(0);
            boolean state = bundle.getBoolean("",false);
            boolean mediaState = bundle.getBoolean("mediaState", false);
            boolean read = bundle.getBoolean("read", false);
            if (state){
                helper.setVisible(R.id.pb_loading, data.getStatus() == 0);
            }
            if (mediaState) {
                AnimationDrawable animationDrawable = (AnimationDrawable) helper.getImageView(R.id.iv_voice_anim).getDrawable();
                //媒体信息状态 0：正常 1：播放  2：暂停
                if (data.getMediaState() == 1) {
                    animationDrawable.start();
                } else {
                    if (animationDrawable.isRunning()) {
                        animationDrawable.stop();
                    }
                }
            }
            if (read) {
                helper.setVisible(R.id.iv_audio_read_status, data.getMsgRead() == 0);
            }
        }
    }

    private String getAudioLength(int length) {
        if (length < 0) return "0''";
        if (length > 60) return (length / 60) + "'" + (length % 60) + "''";
        return (length % 60) + "''";
    }
}
